package 算法练习;/**
 * @author： li
 * @date： 2022-03-21 16:00
 * @version 1.0
 */
import java.util.*;
public class 入学考试{
    public static void main(String[] args){
        int n,v;
        Scanner sc = new Scanner(System.in);
        v = sc.nextInt();
        n = sc.nextInt();
        int[] v1 = new int[n+1];
        int[] w = new int[n+1];
        int[] f = new int[v+1];
        for(int i=1;i<=n;i++){

            v1[i]= sc.nextInt();
            w[i] = sc.nextInt();
        }
        for(int i=1;i<=n;i++){
            for(int j=v;j>=v1[i];j--)
                f[j]=Math.max(f[j],f[j-v1[i]]+w[i]);
        }
        sc.close();
        System.out.println(f[v]);
    }
}